package BookFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class BookTool extends JFrame implements ActionListener {
    private JPanel centerPanel = new JPanel();
    private JTextField tfResult = new JTextField(); // 结果显示
    private String operation = ""; // 当前操作类型（库存计算/逾期罚款）
    private double value1 = 0; // 操作数1（如借阅天数）
    private double value2 = 0; // 操作数2（如逾期天数）

    public BookTool() {
        setTitle("图书管理工具");
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(400, 400);
        setLocationRelativeTo(null); // 窗口居中

        // 顶部结果显示栏
        tfResult.setHorizontalAlignment(JTextField.RIGHT);
        tfResult.setFont(new java.awt.Font("宋体", 0, 18));
        tfResult.setText("请选择操作");
        add(tfResult, BorderLayout.NORTH);

        // 中心功能按钮面板
        centerPanel.setLayout(new GridLayout(5, 4));
        addButton("库存增加", Color.GREEN);
        addButton("库存减少", Color.RED);
        addButton("计算逾期罚款", Color.BLUE);
        addButton("重置", Color.YELLOW);
        addButton("0", Color.LIGHT_GRAY);
        addButton("1", Color.LIGHT_GRAY);
        addButton("2", Color.LIGHT_GRAY);
        addButton("3", Color.LIGHT_GRAY);
        addButton("4", Color.LIGHT_GRAY);
        addButton("5", Color.LIGHT_GRAY);
        addButton("6", Color.LIGHT_GRAY);
        addButton("7", Color.LIGHT_GRAY);
        addButton("8", Color.LIGHT_GRAY);
        addButton("9", Color.LIGHT_GRAY);
        addButton(".", Color.LIGHT_GRAY);
        addButton("=", Color.BLACK);
        add(centerPanel, BorderLayout.CENTER);
    }

    /** 添加功能按钮 */
    private void addButton(String name, Color color) {
        JButton btn = new JButton(name);
        btn.setBackground(color);
        btn.setForeground(Color.WHITE);
        btn.addActionListener(this);
        centerPanel.add(btn);
    }

    /** 库存计算逻辑 */
    private void calculateStock(boolean isAdd, double amount) {
        double result = isAdd ? value1 + amount : value1 - amount;
        tfResult.setText(String.format("库存更新为：%.0f", result));
        value1 = result; // 保留最新库存值
    }

    /** 逾期罚款计算逻辑（默认每天1元） */
    private void calculateOverdueFine(double days) {
        double fine = days * 1.0; // 可配置罚款规则
        tfResult.setText(String.format("逾期罚款：¥%.2f", fine));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        // 操作选择
        if (cmd.equals("库存增加")) {
            operation = "ADD_STOCK";
            tfResult.setText("请输入增加数量");
            value1 = 0; // 重置操作数
        } else if (cmd.equals("库存减少")) {
            operation = "SUB_STOCK";
            tfResult.setText("请输入减少数量");
            value1 = 0;
        } else if (cmd.equals("计算逾期罚款")) {
            operation = "OVERDUE_FINE";
            tfResult.setText("请输入逾期天数");
            value1 = 0;
        } else if (cmd.equals("重置")) {
            tfResult.setText("请选择操作");
            operation = "";
            value1 = 0;
            value2 = 0;
        }

        // 数字输入
        else if (cmd.matches("[0-9.]")) {
            if (operation.isEmpty()) {
                tfResult.setText("请先选择操作");
                return;
            }
            // 拼接数字
            if (tfResult.getText().startsWith("请输入")) {
                tfResult.setText(cmd);
            } else {
                tfResult.setText(tfResult.getText() + cmd);
            }
            // 解析当前输入为数值
            try {
                value1 = Double.parseDouble(tfResult.getText());
            } catch (NumberFormatException ex) {
                tfResult.setText("输入错误");
            }
        }

        // 执行计算
        else if (cmd.equals("=")) {
            if (operation.isEmpty()) return;

            switch (operation) {
                case "ADD_STOCK":
                    calculateStock(true, value1);
                    break;
                case "SUB_STOCK":
                    calculateStock(false, value1);
                    break;
                case "OVERDUE_FINE":
                    calculateOverdueFine(value1);
                    break;
                default:
                    tfResult.setText("未知操作");
            }
            operation = ""; // 重置操作
        }
    }

    public static void main(String[] args) {
        new BookTool().setVisible(true);
    }
}
